// pages/profile-data/index.js
const db = wx.cloud.database()
import {userStore} from '../../store/index'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({
  data: {
    id:"",
    nickname:"",
    password:"",
    avatarUrl:"",
    sex:"",
    columns:['男','女','未知'],
    showSex:false,
    showNickname:false,
    showPassword:false,
    defaultSex:0,
    newSex:"",
    newAvatarUrl:"",
    newNickname:"",
    newPassword:"",
    newConfirmPassword:""
  },

  onLoad: function (options) {
    this.getUserInfo()
  },
  getUserInfo(){
    userStore.onStates(["id","nickname","avatarUrl","password","sex"],({
      id,
      nickname,
      avatarUrl,
      password,
      sex
    })=>{
      if(id) this.setData({id})
      if(nickname) this.setData({nickname})
      if(avatarUrl) this.setData({avatarUrl})
      if(password) this.setData({password})
      if(sex) this.setData({sex})
    })
  },
  //===============================修改头像===================================
  uploadImageClick(){
    const that = this
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album','camera'],
      sizeType:['compressed'],
      camera:'back',
      success(res){
        var filePath = res.tempFiles[0].tempFilePath;
        that.cloudUpload(filePath)
      }
    })

  },
  cloudUpload(filePath){
    var that = this
    wx.cloud.uploadFile({
        cloudPath:Date.now()+".jpg",
        filePath:filePath
    }).then(res=>{
      console.log(res);
      that.setData({
        newAvatarUrl:res.fileID
      })
      that.updateUserImage()
    })
  },

  updateUserImage(){
    db.collection('user').doc(this.data.id).update({
      data:{
        avatarUrl:this.data.newAvatarUrl
      },
    })
    userStore.setState("avatarUrl",this.data.newAvatarUrl)
    Toast('头像修改成功')
  },

  //===================================修改昵称=================================
  onChangeNickname(event){
    var newNickname = event.detail
    this.setData({
      newNickname
    })
  },
  showPopupNickname() {
    this.setData({ 
      showNickname: true,
     });
  },

  onCloseNickname() {
    this.setData({
       showNickname: false,
      });
  },

  cancelNicknameClick(){
    this.setData({
       showNickname: false,
      });
  },

  confirmNicknameClick(){
    if(this.data.newNickname == this.data.nickname){
      Toast('请修改昵称')
    }
    else if(!this.data.newNickname){
      Toast('昵称不能为空')
    }
    else{
      db.collection('user').doc(this.data.id).update({
        data:{
          nickname:this.data.newNickname
        },
      })
      userStore.setState("nickname",this.data.newNickname)
      this.setData({
         showNickname: false, 
      });
      Toast('修改成功')
    }
    
  },

  //===================================性别======================================
  onChangeSex(event) {
    const {value} = event.detail;
    this.setData({newSex:value})
  },
  //弹出性别框
  showPopupSex() {
    if(this.data.sex == '男'){
      this.setData({defaultSex:0})
    }
    if(this.data.sex == '女'){
      this.setData({defaultSex:1})
    }
    if(this.data.sex == '未知')
    {
      this.setData({defaultSex:2})
    }
    this.setData({ showSex: true });
  },

  onCloseSex() {
    this.setData({ showSex: false })
  },
  //性别确认
  onConfirmSex(event) {
    const {value} = event.detail;
    this.setData({newSex:value})
    db.collection('user').doc(this.data.id).update({
      data:{
        sex:this.data.newSex
      },
    })
    userStore.setState("sex",this.data.newSex)
    this.setData({ 
      showSex: false,
     })
    Toast('修改成功')
  },

  onCancelSex() {
    this.setData({ showSex: false })
  },

  //========================================修改密码=====================================
  onChangePassword(event){
    var newPassword = event.detail
    this.setData({
      newPassword
    })
  },
  onChangeConfirmPassword(event){
    var newConfirmPassword = event.detail
    this.setData({
      newConfirmPassword
    })
  },
  showPopupPassword() {
    this.setData({ 
      showPassword: true,
     });
  },
  onClosePassword() {
    this.setData({
       showPassword: false,
      });
  },
  cancelPasswordClick(){
    this.setData({
       showPassword: false,
      });
  },
  confirmPasswordClick(){
    if(this.data.newPassword == this.data.password && this.data.newConfirmPassword == this.data.password){
      Toast('与原密码相同')
    }
    else if(!this.data.newPassword || !this.data.newConfirmPassword){
      Toast('密码不能为空')
    }
    else if(this.data.newConfirmPassword !== this.data.newPassword){
      Toast('两次密码不一致')
      this.setData({
        newConfirmPassword:""
      })
    }
    else{

        db.collection('user').doc(this.data.id).update({
          data:{
            password:this.data.newPassword
          },
        })
        userStore.setState("password",this.data.newPassword)
        this.setData({
           showPassword: false, 
           newPassword:"",
           newConfirmPassword:""
        });
        Toast('修改成功')
      
    }
    
  },
})